package icu.bughub.app.app.ui.components.video

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import com.tencent.rtmp.ITXVodPlayListener
import com.tencent.rtmp.TXLiveConstants
import com.tencent.rtmp.TXVodConstants
import com.tencent.rtmp.TXVodPlayer

class VodController(
    context: Context,
    val playerValue: PlayerValue
) {
    val vodPlayer = TXVodPlayer(context).apply {
        setVodListener(object : ITXVodPlayListener {
            override fun onPlayEvent(
                player: TXVodPlayer?,
                event: Int,
                param: Bundle?
            ) {
                when (event) {
                    TXLiveConstants.PLAY_EVT_PLAY_LOADING -> {
                        playerValue.state = PlayState.LOADING
                    }

                    TXLiveConstants.PLAY_EVT_VOD_PLAY_PREPARED, TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME,
                    TXLiveConstants.PLAY_EVT_VOD_LOADING_END
                        -> {
                        playerValue.state = PlayState.PLAYING
                    }

                    TXLiveConstants.PLAY_EVT_PLAY_BEGIN
                        -> {
                        playerValue.state = PlayState.PLAYING
                    }

                    // 获取视频时长和进度
                    TXLiveConstants.PLAY_EVT_PLAY_PROGRESS -> {
                        val playable_duration_ms =
                            param?.getInt(TXLiveConstants.EVT_PLAYABLE_DURATION_MS);
                        //mLoadBar.setProgress(playable_duration_ms); // 设置loading 进度条
                        // 播放进度, 单位是毫秒
                        val progress_ms = param?.getInt(TXLiveConstants.EVT_PLAY_PROGRESS_MS)
                        //mSeekBar.setProgress(progress_ms);  // 设置播放进度条
                        // 视频总长, 单位是毫秒
                        val duration_ms = param?.getInt(TXLiveConstants.EVT_PLAY_DURATION_MS)
                        // 可以用于设置时长显示等等
                        // 获取 PDT 时间，播放器高级版 11.6 版本开始支持
                        val pdt_time_ms = param?.getLong(TXVodConstants.EVT_PLAY_PDT_TIME_MS)
                        playerValue.duration =
                            param?.getInt(TXLiveConstants.EVT_PLAY_DURATION_MS)?.toLong() ?: 0L
                        playerValue.currentPosition =
                            param?.getInt(TXLiveConstants.EVT_PLAY_PROGRESS_MS)?.toLong() ?: 0L

                    }
                }
            }

            override fun onNetStatus(player: TXVodPlayer?, params: Bundle?) {
            }

        })
    }

    // 播放视频
    fun startVodPlay() {
        playerValue.videoUrl = playerValue.videoUrl
        // 实现视频播放逻辑
        if (TextUtils.isEmpty(playerValue.videoUrl)) {
            return
        }
        vodPlayer.startVodPlay(playerValue.videoUrl)
    }

    // 暂停播放
    fun pause() {
        vodPlayer.pause()
        playerValue.state = PlayState.PAUSE
    }

    // 停止播放
    fun stopVodPlay() {
        vodPlayer.stopPlay(true)

    }

    /**
     * 配置改变后重新播放
     */
    fun restore() {
        vodPlayer.setStartTime(playerValue.currentPosition / 1000F)
        startVodPlay()
    }

    //恢复播放
    fun resume() {
        vodPlayer.resume()
        playerValue.state = PlayState.PLAYING
    }

    /**
     * 跳转到指定时间
     */
    fun seekTo(millSeconds: Long) {
        vodPlayer.seek((millSeconds / 1000).toInt())
    }


}

/*  remember写法  横竖屏切换会消失
@Composable
fun rememberVodController(
    context: Context = LocalContext.current,
    coverUrl: String? = "",
    videoUrl: String
) =
    remember {
        VodController(context, coverUrl, videoUrl)
    }*/

/*
@Composable
fun rememberVodController(
    context: Context = LocalContext.current,
    coverUrl: String? = "",
    videoUrl: String
) =
    remember {
        val playerValue = PlayerValue().apply {
            this.coverUrl = coverUrl ?: ""
            this.videoUrl = videoUrl
            this.title = title
        }
        VodController(context, playerValue)
    }

*/



@Composable
fun rememberVodController(
    context: Context = LocalContext.current,
    coverUrl: String? = "",
    videoUrl: String? = "",
    title: String = "视频详情"
) =
    rememberSaveable(saver = object : Saver<VodController, PlayerValue> {
        override fun restore(value: PlayerValue): VodController {
            return VodController(context, value)
        }

        override fun SaverScope.save(value: VodController): PlayerValue {
            return value.playerValue
        }

    }) {
        val playerValue = PlayerValue().apply {
            this.coverUrl = coverUrl ?: ""
            this.videoUrl = videoUrl ?: ""
            this.title = title
        }
        return@rememberSaveable VodController(context, playerValue)
    }
